/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.visuals;

import com.kurvykurvy.image.ImageFrameWidget;
import com.kurvykurvy.page.ImageFrame;
import com.kurvykurvy.page.ImageFrameFactory;
import com.kurvykurvy.page.ImageFrameTemplate;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import javax.imageio.ImageIO;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.model.ObjectScene;
import org.netbeans.api.visual.widget.ImageWidget;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;

/**
 *
 * @author HY
 */
public class PageLayoutScene
    extends ObjectScene
{
    BufferedImage pageMaskImage;

    LayerWidget pageMaskLayer;
    LayerWidget labelLayer;
    LayerWidget stickerLayer;
    LayerWidget decorationLayer;
    LayerWidget imageLayer;

    Vector<Widget> imageWidgets;

    public PageLayoutScene()
    {
        pageMaskLayer = new LayerWidget( this );
        pageMaskLayer.setOpaque(  false );
        labelLayer = new LayerWidget( this );
        stickerLayer = new LayerWidget( this );
        decorationLayer = new LayerWidget( this );
        imageLayer = new LayerWidget( this );
        initImageLayer();

        addChild( imageLayer );
        addChild( decorationLayer );
        addChild( stickerLayer );
        addChild( labelLayer );
        addChild( pageMaskLayer );

        debug();
    }

    private void debug()
    {
        try
        {
            BufferedImage pageMask = ImageIO.read( new File( "E:/dev/KurvyXML/bear_shape/book-shape-actual.gif") );
            BufferedImage decoration = ImageIO.read( new File( "E:/dev/KurvyXML/bear_shape/c-actual.gif" ) );
            BufferedImage test = ImageIO.read(  new File( "G:/Documents/photos/1.jpg" ) );

            setPageMask( pageMask );
            setDecoration( decoration );

            ImageFrameTemplate frameTemplate = new ImageFrameTemplate( 0, 0, 1200, 1200 );
            ImageFrame frame = ImageFrameFactory.createImageFrame( frameTemplate, test );
            addImageFrame( frame );
        }
        catch( IOException ioEx )
        {

        }
    }

    public void setPageMask( BufferedImage img )
    {
        if( pageMaskLayer == null )
            pageMaskLayer = new LayerWidget( this );

        pageMaskImage = img;
        Widget pageMaskWidget = new ImageWidget( this, pageMaskImage );
        pageMaskWidget.setCheckClipping(  true );
        pageMaskLayer.addChild( pageMaskWidget );
        this.revalidate();
    }

    public BufferedImage getPageMask()
    {
        return pageMaskImage;
    }

    public void setDecoration( BufferedImage img )
    {
        if( decorationLayer == null )
            decorationLayer = new LayerWidget( this );

        Widget decorationWidget = new ImageWidget( this, img );
        decorationLayer.addChild(  decorationWidget );
        this.revalidate();
    }

    public void addLabel( Point pt, String text )
    {
        if( labelLayer == null )
            labelLayer = new LayerWidget( this );

        Widget label = new LabelWidget( this, text );
        label.setPreferredLocation( convertLocalToScene(  pt ) );
        this.revalidate();
    }

    public void removeLabel( Widget widget )
    {
        if( labelLayer != null )
        {
            labelLayer.removeChild( widget );
        }
    }
    
    public void addSticker( Point pt, BufferedImage img )
    {
        if( stickerLayer == null )
            stickerLayer = new LayerWidget( this );
        
        Widget sticker = new ImageWidget( this, img );
        stickerLayer.addChild( sticker );
        this.revalidate();
    }

    public void removeSticker( Widget widget )
    {
        if( stickerLayer != null )
        {
            stickerLayer.removeChild( widget );
        }
    }

    private void initImageLayer()
    {
        if( imageWidgets == null )
            imageWidgets = new Vector<Widget>();
    }

    public void addImageFrame( ImageFrame frame )
    {
        if( imageWidgets == null )
            imageWidgets = new Vector<Widget>();
        ImageFrameWidget imageWidget = new ImageFrameWidget( this, frame );
        imageWidget.getActions().addAction(  ActionFactory.createResizeAction() );
        imageWidget.getActions().addAction( ActionFactory.createMoveAction() );
        imageWidget.getActions().addAction( this.createSelectAction() );
        imageWidgets.add( imageWidget );
        imageLayer.addChild(  imageWidget );
        revalidate();
    }


}
